<template>
  <div>
    <el-card class="member-box-card">
      <el-input id="input-field" v-model="search" size="mini" placeholder="输入流水号进行搜索"/>
      <el-table id="member-table" ref="memberTable" :key="itemKey" v-loading="loading"  border height="700"
                style="width: 100%" :data="searchHandler()" max-height="450" >

        <el-table-column prop="department" label="部门名称" width="120"/>
        <el-table-column prop="acceptId" label="编号/流水号"  width="120" />
        <el-table-column prop="provider" label="供货单位" width="120"/>
        <el-table-column prop="invoice" label="发票号" width="120"/>
<!--        <el-table-column prop="contract" label="合同号" width="120"/>-->
        <el-table-column prop="place" label="到货地点" width="120"/>
        <el-table-column prop="status" label="验收情况" width="120"/>
        <el-table-column prop="id" label="序号"  width="60"/>
        <el-table-column align="right"  fixed="right" width="250">
          <template slot-scope="scope">
            <el-button size="mini"
              @click="handleAccept(scope.$index, scope.row)"
            >验收</el-button>
            <el-button size="mini" type="danger"
              @click="handleReceive(scope.$index, scope.row)"
            >点收</el-button>
            <el-button size="mini" type="danger"
                       @click="handleReturn(scope.$index, scope.row)"
            >退料</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

  </div>
</template>

<script>
  import { selectByOk, exportAccept, exportReceive, exportReturn,selectByacId} from '@/api/entrywarehouse/accept-inform'
  import {addGood, editGood, removeGood, selectById} from '@/api/entrywarehouse/accept-goods'
  import { getToken } from '@/utils/auth'
  import XLSX2 from "xlsx";
  import XLSX from "xlsx-style";
  import FileSaver from 'file-saver';

  export default {
    name: 'index',
    data() {
      return {
        fillData: [],
        tableData: [],
        total1: 0,
        total2: 0,
        total3: 0,
        total4: 0,
        search: '',
        itemKey: '',
        loading: false,
        headers: {
          Authorization: getToken()
        }
      }
    },
    created() {
      this.init()
    },
    methods: {
      init() {
        selectByOk().then((response) => {
          this.tableData = response.data
        })
      },
      searchHandler() {
        return this.tableData.filter(data => !this.search || data.acceptId.toLowerCase().includes(this.search.toLowerCase()))
      },

      handleAccept(index, row) {
        selectById(row.acceptId).then((response) => {
          this.fillData = response.data
        })

        require.ensure([], () => {
          const { export_json_to_excelaccept} = require("@/excelentry/export2Excel")
          let list = this.fillData;
          let index = 1;
          for (let i= 0; i < list.length; i++){
            list[i].id = index;
            index = index + 1;
            // list[i].push({'money1': list[i].price*list[i].yesNum , 'money2': list[i].price*list[i].noNum ,
            //   'money3': list[i].price*list[i].shortage , 'money4': list[i].price*list[i].surplus})
            list[i].money1 = list[i].price*list[i].yesNum;
            list[i].money2 = list[i].price*list[i].noNum;
            list[i].money3 = list[i].price*list[i].shortage;
            list[i].money4 = list[i].price*list[i].surplus;
            if (list[i].remark == null){
              list[i].remark = " "
            }
            this.total1 =  this.total1 + list[i].money1;
            this.total2 =  this.total2 + list[i].money2;
            this.total3 =  this.total3 + list[i].money3;
            this.total4 =  this.total4 + list[i].money4;
          }


          const title1 = [
            "物资验收记录表","","","","","","","","","","","","","",""
          ]
          const title2 = [
            "单位名称：" + row.department,"","","","","    年   月  日","","","","","","","","编号："+ row.acceptId,""
          ]
          const title3 = [
            "供货单位："+row.provider,"","","","合同号：","","","","到货地点："+row.place,"","","","发票号："+row.invoice,"",""
          ]
          const title4 = [
            "序号","物资名称","规格型号","计量单位","单价（元）","账单所列数量","合格","","不合格","","短少","","多余","","备注"
          ]
          const title5 = [
            "","","","","","","数量","金额","数量","金额","数量","金额","数量","金额",""
          ]

          const filterVal = [
            "id",
            "name",
            "specification",
            "unit",
            "price",
            "billNum",
            "yesNum",
            "money1",
            "noNum",
            "money2",
            "shortage",
            "money3",
            "surplus",
            "money4",
            "remark"
          ]

          const  end2 = [
            "物资验收情况及意见","","","","","","","","","","","","","",""
          ]
          const  end1 = [
            "","合计","","","","","", this.total1,"", this.total2,"", this.total3,"", this.total4,""
          ]
          const  end3 = [
            "检测中心：","","","","","验收：","","","","","库管：","","","",""
          ]

          const data = this.formatJson(filterVal, list)

          export_json_to_excelaccept(title1, title2, title3, title4, title5, data, "物资验收记录表", end1, end2, end3)
        }).catch((err) => {
          console.log("导出失败");
        })
      },

      handleReceive(index, row) {
        selectById(row.acceptId).then((response) => {
          this.fillData = response.data
        })

        require.ensure([], () => {
          const { export_json_to_excelreceive} = require("@/excelentry/export2Excel")
          let list = this.fillData;
          let index = 1;
          for (let i= 0; i < list.length; i++){
            list[i].id = index;
            index = index + 1;
            list[i].money1 = list[i].price*list[i].yesNum;
            list[i].money2 = list[i].price*list[i].yesNum*list[i].tax;
            list[i].remark = " "

            this.total1 =  this.total1 + list[i].money1;
            this.total2 =  this.total2 + list[i].money2;
          }


          const title1 = [
            "物资收料单","","","","","","","","","",""
          ]
          const title2 = [
            "收货单位：" + row.department,"","","","      年     月    日","","","","","",""
          ]
          const title3 = [
            "供货单位："+row.provider,"","","","","","","","编号："+row.acceptId,"",""
          ]
          const title4 = [
            "序号","物资名称","规格型号","单位","数量","","金额","","税额","","备注"
          ]
          const title5 = [
            "","","","","应收","实收","单价","金额","税率","税额",""
          ]

          const filterVal = [
            "id",
            "name",
            "specification",
            "unit",
            "billNum",
            "yesNum",
            "price",
            "money1",
            "tax",
            "money2",
            "remark"
          ]

          const  end1 = [
            "","合计","","","","","", this.total1,"", this.total2,""
          ]
          const  end2 = [
            "业务：","","","","财务：","","","","库管：","",""
          ]

          const data = this.formatJson(filterVal, list)

          export_json_to_excelreceive(title1, title2, title3, title4, title5, data, "物资收料单表", end1, end2)
        }).catch((err) => {
          console.log("导出失败");
        })

      },

      handleReturn(index, row) {
        selectById(row.acceptId).then((response) => {
          this.fillData = response.data
        })

        require.ensure([], () => {
          const { export_json_to_excelreturn} = require("@/excelentry/export2Excel")
          let list = this.fillData;
          let index = 1;
          for (let i= 0; i < list.length; i++){
            if(list[i].noNum != 0){
              list[i].id = index;
              index = index + 1;
              list[i].reason = " ";
              list[i].remark = " "
            }else{
              list[i].id = null;
              list[i].name = null;
              list[i].specification = null;
              list[i].unit = null;
              list[i].noNum = null;
              list[i].reason = null;
              list[i].remark = null;
            }
          }

          const title1 = [
            "物资退料单","","","","","",""
          ]
          const title2 = [
            "供料单位：" + row.provider,"","","    年   月  日","","",""
          ]
          const title3 = [
            "退货部门："+row.department,"","","","","编号："+row.acceptId,""
          ]
          const title4 = [
            "序号","物资名称","规格型号","计量单位","数量","退料原因","备注"
          ]

          const filterVal = [
            "id",
            "name",
            "specification",
            "unit",
            "noNum",
            "reason",
            "remark"
          ]

          const  end1 = [
            "说明","","","","","",""
          ]
          const  end2 = [
            "","","","","","退料人：",""
          ]

          const data = this.formatJson(filterVal, list)

          export_json_to_excelreturn(title1, title2, title3, title4, data, "物资退料单表", end1, end2)
        }).catch((err) => {
          this.$message({
            type: 'info',
            message: '导出失败或者不存在退料'
          })
          console.log("导出失败");
        })

      },

      //格式转换,不需要改动
      formatJson(filterVal, jsonData) {
        // console.log(jsonData)
        // console.log(this.total1)
        // debugger
        this.fillData = ''
        this.total1 = 0
        this.total2 = 0
        this.total3 = 0
        this.total4 = 0
        return jsonData.map((v) => filterVal.map((j) => v[j]));

      }

    }
  }
</script>

<style >
  .member-box-card {
    width: 95%;
    margin: 20px auto;
  }
  #input-field {
    width: 200px;
    display: inline;
  }
  #member-table {
    margin-top: 5px;
  }
  .action-form {
    width: 400px;
  }
</style>
